<?php
/**
 * Created by PhpStorm.
 * User: ewen
 * Date: 4/27/21
 * Time: 9:58 AM
 */

namespace app\common\model;


use think\Db;
use think\Exception;

class WebAssetAccount extends Common
{

    /**
     * 用户钱包资产表
     */


    /**
     * 获取用户指定币种余额
     * @param $coinSymbol
     * @param $userID
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getUserCoinAmount($coinSymbol, $userID)
    {
        !$userID && backError(lang('invalid user id'));
        $coinInfo = CoreCurrency::getCoinInfoByCoinSymbol($coinSymbol);

        $amountInfo = self::where([
            'user_id' => $userID,
            'currency_id' => $coinInfo['currency_id']
        ])->find();
        if (!$amountInfo) {
            //  如果没有钱包信息则创建之后返回
            $res = self::add([
                'user_id' => $userID,
                'currency_id' => $coinInfo['currency_id']
            ]);
            !$res && backError(lang('wallet created failed'));
            $amountInfo = self::where([
                'user_id' => $userID,
                'currency_id' => $coinInfo['currency_id']
            ])->find();
        }
        return $amountInfo;
    }


    /**
     * 钱包余额变化
     * @param $userID
     * @param $coinSymbol
     * @param $num
     * @param int $inOrOut
     */
    public static function coinAmountChange($userID, $coinSymbol, $num, $inOrOut = self::TYPE_INC, $flowTyoe = 100)
    {
        $wallet = self::getUserCoinAmount($coinSymbol, $userID);
        if ($inOrOut == self::TYPE_INC) {
            //  增加
            $wallet['available'] += $num;
        } else {
            //  减少
            $wallet['available'] < $num && backError(lang('lack of balance'));
            $wallet['available'] -= $num;
        }
        Db::startTrans();
        try {
            //  钱包余额变化
            $res = self::where([
                'id' => $wallet['id']
            ])->save([
                'available' => $wallet['available']
            ]);
            if (!$res) throw new Exception(lang('balance edit failed'));

            //  新增变化记录
            $inOrOut == self::TYPE_DEC && $num = plus_minus_conversion($num);
            $res = AssetFlow::addFlowLog($userID, $wallet['currency_id'], $num, $wallet['available'], $flowTyoe);
            if (!$res) throw new Exception(lang('data insert failed'));

            Db::commit();
            return ['status' => 1, 'msg' => 'OK'];
        } catch (Exception $e) {
            Db::rollback();
            return ['status' => 0, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 奖励TXUSD
     * @param $doneNum -完成数量
     * @param $needNum -需要数量
     * @param $award -奖励数量
     * @param $flowType -奖励类型
     * @param $userID -用户ID
     */
    public static function awardTxUsd($doneNum, $needNum, $award, $flowType, $userID)
    {
        //  判断奖励是否溢出
        AssetFlow::getFlowTypeSumIncome($userID);
        //  数量完成奖励TXUSD
        if ($award && $doneNum >= $needNum) {
            //  查看是否已经给过奖励
            $res = AssetFlow::where([
                'currency_id' => 1000002,
                'flow_type' => $flowType,
                'user_id' => $userID
            ])->count();
            if (!$res) {
                WebAssetAccount::coinAmountChange($userID, 'TXUSD', $award, self::TYPE_INC, $flowType);
            }
        }
    }

}